﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Admin_Components_Customer_SaleOrderList : System.Web.UI.Page
{
    private DataTable tblresult = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int PageNumber = gv.CurrentPageIndex + 1;
            int PageSize = gv.PageSize;
            if (Request.QueryString["CustomerID"] != null && Session["UserName"] != null)
            {
                string customerCode = Request.QueryString["CustomerID"].ToString();
                
                ViewState["CustomerCode"] = customerCode;
                loadData(PageNumber, PageSize);
            }
            else if (Request.QueryString["DeilveryEmployeeID"] != null && Session["UserName"] != null)
            {
                string EmployeeID = Request.QueryString["DeilveryEmployeeID"].ToString();
                ViewState["DeilveryEmployeeID"] = EmployeeID;
                string FromDate = Request.QueryString["FromDate"].ToString();
                ViewState["FromDate"] = FromDate;
                string ToDate = Request.QueryString["ToDate"].ToString();
                ViewState["ToDate"] = ToDate;
                loadData(PageNumber, PageSize);

            }
            else if (Request.QueryString["SaleEmployeeID"] != null && Session["UserName"] != null)
            {
                string EmployeeID = Request.QueryString["SaleEmployeeID"].ToString();
                ViewState["SaleEmployeeID"] = EmployeeID;
                string FromDate = Request.QueryString["FromDate"].ToString();
                ViewState["FromDate"] = FromDate;
                string ToDate = Request.QueryString["ToDate"].ToString();
                ViewState["ToDate"] = ToDate;
                loadData(PageNumber, PageSize);

            }
            else if (Request.QueryString["ShopID"] != null)
            {
                string ShopID = Request.QueryString["ShopID"].ToString();
                ViewState["ShopID"] = ShopID;
                string FromDate = Request.QueryString["FromDate"].ToString();
                ViewState["FromDate"] = FromDate;
                string ToDate = Request.QueryString["ToDate"].ToString();
                ViewState["ToDate"] = ToDate;
                loadData(PageNumber, PageSize);
            }
        }
    }

    private void loadData( int PageNumber,int PageSize)
    {
       // gv.CurrentPageIndex = 0;
        DBDataContext db = LINQUtil.GetDBWriteOnly();
        if (ViewState["ShopID"] == null)
        {
            string customerCode = ViewState["CustomerCode"] != null ? ViewState["CustomerCode"].ToString() : "";
            string DeliveryEmployeeID = ViewState["DeilveryEmployeeID"] != null ? ViewState["DeilveryEmployeeID"].ToString() : "0";
            string SaleEmployeeID = ViewState["SaleEmployeeID"] != null ? ViewState["SaleEmployeeID"].ToString() : "0";
            //DateTime FromDate = new DateTime(2014, 06, 01);
            //DateTime ToDate = new DateTime(2099, 12, 31);
            DateTime FromDate = DateTime.Parse(ViewState["FromDate"] == null?"2014-01-01": ViewState["FromDate"].ToString());
            DateTime ToDate = DateTime.Parse(ViewState["ToDate"] == null ? "2099-01-01" : ViewState["ToDate"].ToString());
            
            var q = db.SaleOrder_Search("", "", 0, 0, customerCode, DeliveryEmployeeID, "", "", "", "", FromDate, ToDate, PageSize, PageNumber, "", SaleEmployeeID).ToList();
            if (q.Count > 0)
            {
                gv.ClientSettings.Scrolling.AllowScroll = true;
                gv.ClientSettings.Scrolling.UseStaticHeaders = true;
                gv.VirtualItemCount = q[0].TotalRows.Value;
                gv.DataSource = q;

                tblresult = q.ToDataTable();
            }
            else
            {
                gv.DataSource = new DataTable();
            }
        }
        else
        {
            DateTime FromDate = DateTime.Parse(ViewState["FromDate"].ToString());
            DateTime ToDate = DateTime.Parse(ViewState["ToDate"].ToString());
           // int PageNumber = gv.CurrentPageIndex + 1;
            //int PageSize = gv.PageSize;
            int ShopID = int.Parse( ViewState["ShopID"].ToString() );
            var q = db.SaleOrderDiscount_Search("C", 0, ShopID, FromDate, ToDate, PageSize, PageNumber).ToList();
            if (q.Count > 0)
            {
                gv.ClientSettings.Scrolling.AllowScroll = true;
                gv.ClientSettings.Scrolling.UseStaticHeaders = true;
                gv.VirtualItemCount = q[0].TotalRows.Value;
                gv.DataSource = q;
                tblresult = q.ToDataTable();
            }
            else
            {
                gv.DataSource = new DataTable();
            }
        }
        gv.DataBind();
    }

    protected void gvData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        int PageNumber = gv.CurrentPageIndex + 1;
        int PageSize = gv.PageSize;
        loadData(PageNumber, PageSize);
    }

    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        ExportExcel();
    }

    protected void ExportExcel()
    {
        loadData(1, 60000);
        if (tblresult.Rows.Count > 0)
        {
            ExcelUtil.ExportDataTableToExcel2007FromRadGrid(tblresult, gv, "danh_sach_don_hang");
        }
        else
        {
            JavascriptUtil.LoadMessage(Page, false, "Không có dữ liệu xuất Excel !!!");

        }
    }
}